package com.ruoyi.web.controller.kubernetes;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.web.domain.KubernetesUserExporter;
import com.ruoyi.web.domain.config.KubernetesConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.KubernetesUser;
import com.ruoyi.web.service.IKubernetesUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * K8s用户Controller
 *
 * @author xuchengchao
 * @date 2022-04-17
 */
@Slf4j
@RestController
@RequestMapping("/kubernetes/user")
public class KubernetesUserController extends BaseController
{
    @Autowired
    private IKubernetesUserService kubernetesUserService;

    /**
     * 查询K8s用户列表
     */
    @PreAuthorize("@ss.hasPermi('kubernetes:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(KubernetesUser kubernetesUser)
    {
        startPage();
        List<KubernetesUser> list = kubernetesUserService.selectKubernetesUserList(kubernetesUser);
        return getDataTable(list);
    }

    /**
     * 导出K8s用户列表
     */
    @PreAuthorize("@ss.hasPermi('kubernetes:user:export')")
    @Log(title = "K8s用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KubernetesUser kubernetesUser)
    {
        List<KubernetesUser> list = kubernetesUserService.selectKubernetesUserList(kubernetesUser);
        ExcelUtil<KubernetesUser> util = new ExcelUtil<KubernetesUser>(KubernetesUser.class);
        util.exportExcel(response, list, "K8s用户数据");
    }

    /**
     * 获取K8s用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('kubernetes:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(kubernetesUserService.selectKubernetesUserById(id));
    }

    /**
     * 新增K8s用户
     */
    @PreAuthorize("@ss.hasPermi('kubernetes:user:add')")
    @Log(title = "K8s用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KubernetesUser kubernetesUser)
    {
        return toAjax(kubernetesUserService.insertKubernetesUser(kubernetesUser));
    }

    /**
     * 修改K8s用户
     */
    @PreAuthorize("@ss.hasPermi('kubernetes:user:edit')")
    @Log(title = "K8s用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KubernetesUser kubernetesUser)
    {
        return toAjax(kubernetesUserService.updateKubernetesUser(kubernetesUser));
    }

    /**
     * 删除K8s用户
     */
    @PreAuthorize("@ss.hasPermi('kubernetes:user:remove')")
    @Log(title = "K8s用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(kubernetesUserService.deleteKubernetesUserByIds(ids));
    }

    @ResponseBody
    @GetMapping("/download/{id}")
    public KubernetesUserExporter download(@PathVariable("id") Long id){
        KubernetesConfig kubernetesConfig = kubernetesUserService.getKubernetesConfig(id);
        JSONObject jsonObject = (JSONObject)JSONObject.toJSON(kubernetesConfig);
        jsonObject.put("current-context","external");
        KubernetesUserExporter kubernetesUserExporter = new KubernetesUserExporter();
        kubernetesUserExporter.setStatus(200);
        kubernetesUserExporter.setData(jsonObject.toJSONString());
        return kubernetesUserExporter;
    }
}
